/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tika.extractor;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

/**
 * Base class for text extractors that need to delegate the extraction
 * of parts of content documents to another text extractor. Subclasses
 * use the delegate extractor for extracting content from composite
 * multimedia or archive file formats.
 * <p>
 * The configured delegate text extractor is usually a composite extractor
 * that may contain also the delegating extractor, thus it is possible for
 * the extractor to be invoked recursively within a single thread. A subclass
 * should be careful to avoid infinite loops.
 */
public class DelegatingTextExtractor implements TextExtractor {

    /**
     * The delegate text extractor.
     */
    private final TextExtractor extractor;

    /**
     * Creates a delegating text textractor. Partial text extractor tasks
     * will be delegated to given text extractor. It is expected that the
     * given extractor is able to handle any content types passed to it.
     *
     * @param extractor delegate text extractor
     */
    protected DelegatingTextExtractor(TextExtractor extractor) {
        this.extractor = extractor;
    }

    //-------------------------------------------------------< TextExtractor >

    /**
     * Delegates the call to the underlying text extractor. Subclasses
     * should override this method to return the supported content types.
     *
     * @return content types supported by the delegate text extractor
     */
    public String[] getContentTypes() {
        return extractor.getContentTypes();
    }

    /**
     * Delegates the call to the underlying text extractor. Subclasses
     * should override this method to correctly extract text from the
     * given binary document.
     * <p>
     * Subclasses should use this method as <code>super.extractText()</code>
     * to access the delegate text extractor.
     *
     * @param stream binary document
     * @param type content type
     * @param encoding character encoding, or <code>null</code>
     * @return text extracted by the delegate text extractor
     */
    public Reader extractText(InputStream stream, String type, String encoding)
            throws IOException {
        return extractor.extractText(stream, type, encoding);
    }

}
